<?php
// mod文件只能被入口文件引用，不能直接访问
if(!defined('IN_DISCUZ')) {
	exit('Access Denied');
}

$topicid = $_G["gp_topicid"];
$articleid = $_G["gp_articleid"];
$article_title = '';

$article_list = array();


$sql = "select * from t_article where topic_id = $topicid";

$query = DB::query($sql.' order by id');
while($row = DB::fetch($query)) {
	$article_list[] =  $row;

	if( $articleid ==  $row["id"] ){
		$article_title = $row["title"];
	}
}

$min_article_id = $article_list[ 0 ][ 'id' ];
$max_article_id = $article_list[ count($article_list) - 1 ]['id'];


if(!isset($articleid) || $articleid > $max_article_id || $articleid < $min_article_id ) {
	$articleid = $min_article_id;
	$article_title = $article_list[ 0 ][ 'title' ];
}

$paragraph_list = array();
$paragraph_sql = " select t.id, t.article_id, t.title, t.content from t_pregnancy_food t where t.article_id = $articleid ";
$paragraph_query = DB::query($paragraph_sql);

$food_title = '';
$food_content = '';
while($paragraph_row = DB::fetch($paragraph_query)) {
	if ( !empty($paragraph_row['title']) ) {
		if ( !empty($food_title) ) {
			$paragraph_list[] = array('title' => $food_title, 'content' => $food_content );
				
			$food_title = '';
			$food_content = '';
		}

		$food_title = $paragraph_row['title'];
	}
	
	if ( !empty($paragraph_row['content']) ) {
		if (strpos($paragraph_row['content'], "：")) {
			$food_content .= ( "<b>". str_replace("：", "：</b>", $paragraph_row['content'])."<br />" );
		}
		else {
			$food_content .= ( $paragraph_row['content']."<br />" );
		}
	}
}

$paragraph_list[] = array('title' => $food_title, 'content' => $food_content );

DB::free_result($paragraph_query);


$pre_article_id = $articleid - 1;
$next_article_id = $articleid + 1;

$show_pre_article = false;
$show_next_article = false;

// 该topic文章数量不为空。
if(count($paragraph_list) > 0 ){
	if ($articleid > $min_article_id ){
		$show_pre_article = true;
	}
	if ($articleid < $max_article_id ){
		$show_next_article = true;
	}
}


include_once template("expert/pregnancyfood");

?>